package com.hnjowe.util;

import java.io.InputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @SMTP邮件发送
 * @projectname：cocotour
 * @packagename：com.hnjowe.util
 * @filename：Mail.java
 * @date：2011-3-20 下午04:44:41
 * @author: wangxiuwu
 */
public class Mail {

    private String mail_host;					//邮箱主机*
    private String mail_username;				//邮箱用户*
    private String mail_password;				//邮箱密码*
    private String mail_from;					//发送者*
    private String mail_auth;					//是否认证*   
    private String mail_sendername="乐旅旅行";	//发件人名字
 
    private static Mail instance = null;
    
    private String mail_to;   
    private String mail_subject;   
    private String mail_content; 

    private Mail() {
    }

    /**
     * 返回一个TimeFormat类的单一实例
     * @return m_instance
     */
    public synchronized  static Mail getInstance(){
    	if(instance == null){
    		instance = new Mail();
    	}
    	return instance;
    }
	
	/**
	 * 邮件发送
	 * @throws Exception
	 */
	public void mailSend(){
        try{
        	/*获取属性文件参数*/
        	getMailParams("/mailsend.properties");

        	/*发送邮件参数设置*/
        	Properties props = new Properties(); // 获取系统环境
            
            //进行邮件服务器用户认证
            Authenticator auth=new Authenticator() {   
                protected PasswordAuthentication getPasswordAuthentication(){   
                    return  new PasswordAuthentication(mail_username, mail_password);   
                }
            };

            props.put("mail.smtp.host", mail_host);
            props.put("mail.smtp.auth", mail_auth);
            Session session = Session.getDefaultInstance(props, auth);			//设置session
            
            //建立消息  
            MimeMessage message = new MimeMessage(session);						//设置邮件格式
            message.setFrom(new InternetAddress(mail_from, mail_sendername)); 	//设置邮件发送者地址
            
            Date send_date = new Date();
            message.setSentDate(send_date); 									//设置邮件发送日期
            
            //设置邮件接收人地址
            InternetAddress toAddress = new InternetAddress(mail_to); 			
            message.addRecipient(Message.RecipientType.TO, toAddress);
            
            //设置邮件主题
            message.setSubject(mail_subject);
            
            //设置邮件正文
            message.setText(mail_content);

            /*发送邮件*/
            Transport.send(message);

            System.out.println("#" + send_date + "Send Mail Ok! From:" + mail_from + ", To:" + mail_to); 

        } catch (Exception ex){
            ex.printStackTrace();
        }
    }
    
    @SuppressWarnings("unchecked")
	private void getMailParams(String propfile) {
        Properties m_params = new Properties();
        InputStream in;
        try {
            in = getClass().getResourceAsStream(propfile);
            m_params.load(in);
            Set keyValue = m_params.keySet();
            for (Iterator it = keyValue.iterator(); it.hasNext();) {
                String key = (String) it.next();
                if (key.equals("mail.host")) mail_host = (String)m_params.get("mail.host");
                if (key.equals("mail.username")) mail_username = (String)m_params.get("mail.username");
                if (key.equals("mail.password")) mail_password = (String)m_params.get("mail.password");
                if (key.equals("mail.from")) mail_from = (String)m_params.get("mail.from");
                if (key.equals("mail.auth")) mail_auth = (String)m_params.get("mail.auth");
            }
        } catch (Exception e) {
        	System.out.println("读取属性文件mailsend.properties失败!" + e);
        }
    }

	public String getMail_to() {
		return mail_to;
	}

	public void setMail_to(String mailTo) {
		mail_to = mailTo;
	}

	public String getMail_subject() {
		return mail_subject;
	}

	public void setMail_subject(String mailSubject) {
		mail_subject = mailSubject;
	}

	public String getMail_content() {
		return mail_content;
	}

	public void setMail_content(String mailContent) {
		mail_content = mailContent;
	}

}
